package com.hanbang.chu_xing_gao_zhao.view_dynamic;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;

import com.hanbang.chu_xing_gao_zhao.base.BaseActivity;
import com.hanbang.chu_xing_gao_zhao.entity.DynamicHorozontalListviewData;
import com.hanbang.chu_xing_gao_zhao.utils.SystemTools;
import com.hanbang.cxgz_2.R;

/**
 * 图片查看
 */
public class PhotoActivity extends BaseActivity {
    /**
     * viewPage 的数据
     */
    private ArrayList<View> viewpageData = null;
    private ViewPager pager = null;
    private MyPageAdapter myPageAdapter;
    /**
     * 传过来的对象
     */
    private ArrayList<DynamicHorozontalListviewData> listData;
    /**
     * 点击的位置
     */
    private int position;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.photo_activity);

        initView();
        addData();
        addAdapter();
    }

    /**
     * 初始化view
     */
    private void initView() {
        pager = (ViewPager) findViewById(R.id.vvv);
        viewpageData = new ArrayList<View>();
        listData = new ArrayList<DynamicHorozontalListviewData>();

        Intent intent = getIntent();
        listData = (ArrayList<DynamicHorozontalListviewData>) intent.getSerializableExtra("listdata");
        position = intent.getIntExtra("position", 0);
    }

    /**
     * 为view 集合添加数据
     */
    private void addData() {

        for (int i = 0; i < listData.size(); i++) {

            ImageView img = new ImageView(this);// 构造textView对象
            img.setBackgroundColor(0xff000000);
            bitmapUtils.display(img, SystemTools.getHttpFileUrl(listData.get(i).getImg_url()));
            img.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

            viewpageData.add(img);// 添加view

            img.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                }
            });
        }

    }

    /**
     * viewPage 设置adapter
     */
    private void addAdapter() {
        myPageAdapter = new MyPageAdapter(viewpageData);
        pager.setAdapter(myPageAdapter);
        pager.setCurrentItem(position);


    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
    }

    /**
     * viewPage 的适配器
     *
     * @author yang
     */
    class MyPageAdapter extends PagerAdapter {

        private ArrayList<View> listViews;// content

        private int size;// 页数

        public MyPageAdapter(ArrayList<View> listViews) {// 构造函数
            // 初始化viewpager的时候给的一个页面
            this.listViews = listViews;
            size = listViews == null ? 0 : listViews.size();
        }

        public void setListViews(ArrayList<View> listViews) {// 自己写的一个方法用来添加数据
            this.listViews = listViews;
            size = listViews == null ? 0 : listViews.size();
        }

        public int getCount() {// 返回数量
            return size;
        }

        public int getItemPosition(Object object) {
            return POSITION_NONE;
        }

        public void destroyItem(View arg0, int arg1, Object arg2) {// 销毁view对象

            ((ViewPager) arg0).removeView(listViews.get(arg1 % size));

        }


        public Object instantiateItem(View arg0, int arg1) {// 返回view对象
            try {
                ((ViewPager) arg0).addView(listViews.get(arg1 % size), 0);

            } catch (Exception e) {
            }
            return listViews.get(arg1 % size);
        }

        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == arg1;
        }

    }
}
